package com.zenithmind.library.pojo.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 图书分类视图对象
 * 
 * @author ZenithMind
 * @since 2024-06-14
 */
@Data
@Schema(name = "图书分类视图对象", description = "图书分类信息的视图对象")
public class CategoryVO {

    @Schema(description = "分类ID")
    private String id;

    @Schema(description = "分类名称")
    private String name;

    @Schema(description = "分类编码")
    private String code;

    @Schema(description = "父分类ID")
    private String parentId;

    @Schema(description = "父分类名称")
    private String parentName;

    @Schema(description = "分类描述")
    private String description;

    @Schema(description = "排序号")
    private Integer sortOrder;

    @Schema(description = "分类图标")
    private String icon;

    @Schema(description = "分类颜色")
    private String color;

    @Schema(description = "状态")
    private Integer status;

    @Schema(description = "状态名称")
    private String statusName;

    @Schema(description = "是否叶子节点")
    private Integer isLeaf;

    @Schema(description = "层级深度")
    private Integer level;

    @Schema(description = "分类路径")
    private String path;

    @Schema(description = "图书数量")
    private Long bookCount;

    @Schema(description = "子分类列表")
    private List<CategoryVO> children;

    @Schema(description = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Schema(description = "更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    /**
     * 获取状态名称
     */
    public String getStatusName() {
        if (status == null) {
            return "未知";
        }
        return switch (status) {
            case 0 -> "禁用";
            case 1 -> "启用";
            default -> "未知";
        };
    }

    /**
     * 判断是否为根分类
     */
    public boolean isRootCategory() {
        return parentId == null || "0".equals(parentId);
    }

    /**
     * 判断是否为叶子节点
     */
    public boolean isLeafNode() {
        return isLeaf != null && isLeaf == 1;
    }

    /**
     * 判断是否有子分类
     */
    public boolean hasChildren() {
        return children != null && !children.isEmpty();
    }
}
